#include <stdio.h>
#include <stdlib.h>
#include "cJSON.h"
#include <math.h>

int main()
{
    cJSON *root = cJSON_CreateObject();
    cJSON_AddStringToObject(root, "name", "nick");
    cJSON_AddNumberToObject(root, "age", 18);
    cJSON_AddFalseToObject(root, "isMarried");

    cJSON *addr = cJSON_CreateObject();
    cJSON_AddStringToObject(addr, "city", "beijing");
    cJSON_AddStringToObject(addr, "street", "dongcheng");

    //  向根节点添加创建好的新节点addr
    cJSON_AddItemToObject(root, "addr", addr);

    // 向根节点添加一个数组
    cJSON *arr = cJSON_CreateArray();
    cJSON_AddItemToArray(arr, cJSON_CreateString("hello"));
    cJSON_AddItemToArray(arr, cJSON_CreateString("world"));
    cJSON_AddItemToArray(arr, cJSON_CreateString("cjson"));
    cJSON_AddItemToObject(root, "skill", arr);

    printf("%s\n", cJSON_Print(root));
    return 0;
}

/*

{

    "name":"nick",
    "age":18,
    "isMarried":false,
    "addr": {
        "city": "beijing",
        "street":"dongdajie"
    },
    "skill":["world","cjson"]
}

gcc 01-gen.c cJSON.c -o 01-gen -lm

*/